import numpy as np 
import tensorflow as tf 
import pandas as pd 

import pymysql as sql

# 计算距离
def euclidean_distance_by_tf(vector1, vector2):
    return tf.sqrt(tf.reduce_sum(tf.square(vector1 - vector2)))

# 查询特定用户数据库
def get_sql(uid):
    # 数据库查询 pymysql
    db = sql.connect(host='xmu-maker.cn',user='root', password='zq',port=3306)
    cursor = db.cursor()

    cursor.execute("use films")
    sql_cmd = f'select mid from user_collect where uid = {uid}'
    cursor.execute(sql_cmd)
    col_mid = cursor.fetchall()
    return col_mid
""" 上述代码 查询成功， 数据放在 col_mid tuple """


# 计算用户向量
def compute_vec(col_mid):
    # 读文件
    vec = np.load("films_vec.npy", allow_pickle="TRUE")
    mid = np.load("films_mid.npy", allow_pickle="TRUE")

    # 求均值向量
    col_films_vec = np.zeros(62)
    for each in col_mid:
        index = list(mid).index(each[0])

        col_films_vec += vec[index]
    col_films_vec /= len(col_mid)

    return col_films_vec
    """ 上述代码 输出均值向量 数据放在col_films_vec """
    

def get_vec(uid):
    return compute_vec(get_sql(uid))

# main
if __name__ == "__main__":
    mids = get_sql(5)
    vec = compute_vec(mids)
    print(vec)